<?
    class Principio_Model_User implements PrincipioLab_IDAble
    {
        use PrincipioLab_TCollection;

        private $id;
        private $login;
        private $password;
        private $role;
        private $name;
        private $last_name;
        private $register_date;
        private $last_login;

        const ROLE_ADMIN = 1;
        const ROLE_SUPERADMIN = 2;


        public function __construct(array $values = null)
        {
            if (is_null($values))
            {
                $this->id = -1;
                $this->login = "";
                $this->password = "";
                $this->role = 1;
                $this->name = "";
                $this->last_name = "";
                $this->register_date = "";
                $this->last_login = "";
            }
            else
            {
                $this->id = $values['id'];
                $this->login = $values['login'];
                $this->password = $values['password'];
                $this->role = intval($values['role']);
                $this->name = $values['name'];
                $this->last_name = $values['last_name'];
                $this->register_date = $values['register_date'];
                $this->last_login = $values['last_login'];
            };
        }


        public function _getID() { return $this->id; }


        public static function fromArray(array $values = null)
        {
            return new Principio_Model_User($values);
        }


        private static function crypt($password) { return md5($password); }
        public function getID() { return intval($this->id); }
        public function getLogin() { return $this->login; }
        public function getPassword() { return $this->password; }
        public function getRole() { return intval($this->role); }
        public function getName() { return $this->name; }
        public function getLastName() { return $this->last_name; }
        public function getRegisterDate() { return $this->register_date; }
        public function getLastLogin() { return $this->last_login; }


        public function setID($id)
        {
            $this->id = $id;
            return $this;
        }


        public function setLogin($login)
        {
            $this->login = $login;
            return $this;
        }


        public function setPassword($password)
        {
            $this->password = self::crypt($password);
            return $this;
        }


        public function setRole($role)
        {
            $this->role = intval($role);
            return $this;
        }


        public function setName($name)
        {
            $this->name = $name;
            return $this;
        }


        public function setLastName($last_name)
        {
            $this->last_name = $last_name;
            return $this;
        }


        public function setRegisterDate($register_date)
        {
            $this->register_date = $register_date;
            return $this;
        }


        public function setLastLogin($last_login)
        {
            $this->last_login = $last_login;
            return $this;
        }


        public function add() { return Principio_Model_UserMapper::save($this); }
        public function save() { return Principio_Model_UserMapper::save($this); }
        public function delete() { return Principio_Model_UserMapper::delete($this); }


        public function getRoles()
        {
            return $roles = [
                self::ROLE_ADMIN => 'admin',
                self::ROLE_SUPERADMIN => 'superadmin'
            ];
        }


        public function checkRole() { return (($this->getRole() == self::ROLE_ADMIN) || ($this->getRole() == self::ROLE_SUPERADMIN)); }
    }
?>